#if !defined NES_GLOBALS_INCLUDED
#include "include/nes/nes_globals.inl"
#endif


#define NES_REGISTER_INCLUDED 1

#if defined USE_SQL
new gDbTupleRegister	
#else
new gVaultRegister;
#endif

new gCvarRegisterActive;
new gCvarPassTime;
new gCvarPassDelayTime;

stock isRegisterActive() {
	return get_pcvar_num(gCvarRegisterActive);
}
public client_disconnect_register(id){

}

public client_connect_register(id){
	
}


public plugin_init_register(){
	gCvarRegisterActive = register_cvar("nes_nick_register", "1");
	gCvarPassTime = register_cvar("nes_nick_register_time", "20");
	gCvarPassDelayTime = register_cvar("nes_nick_register_delay_time", "5");
	
	register_clcmd("say /register", "cmdRegisterName");
	register_clcmd("say /unregister", "cmdUnRegisterName");
	register_concmd("register_name_password", "namePassword");
	register_concmd("unregister_name_password", "unregisterName");
	#if defined USE_SQL
	gDbTupleRegister = 0;
	#else
	gVaultRegister = nvault_open("nes_register");
	#endif
}
stock checkUserInfo(id){
	new pass[32];
	new pass2[32];
	get_user_info(id, const_NES_PASS_FLD, pass, charsmax(pass));
	if(getUserPass(id, pass2, charsmax(pass2))){
		
	}
}
stock getUserPass(id, pass[], len){
	new timestamp;
	if(nvault_lookup(gVaultRegister, playerKey[id],pass, len, timestamp)){
		return 1;
	}
	return 0;
}
stock isNickRegistered(id){
	new pass[1];
	return getUserPass(id, pass, 0);
}

public cmdRegisterName(id) {
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isRegisterActive())
		return PLUGIN_CONTINUE;
	
	if(isNickRegistered(id)){
		client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_ALREADY_REGISTERED", name);
	} else {
		set_hudmessage(255, 0, 0, 0.0, 0.03, 0, 6.0, 5.0);
		show_hudmessage(id, "%L", LANG_PLAYER, "ENTER_PASSWORD");
		client_cmd(id, "messagemode register_name_password");
	}
	return PLUGIN_HANDLED;

}

public namePassword(id, level, cid){
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isRegisterActive())
		return PLUGIN_CONTINUE;
	new pass[32];
	new name[MAX_NAME_LEN];
	copy(name, MAX_NAME_LEN -1, playerKey[id]);
	
	if(getUserPass(id, pass, 31)){
		client_print(id, print_console, "%L", LANG_PLAYER, "NICK_ALREADY_REGISTERED", name);
		client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_ALREADY_REGISTERED", name);
	} else {
		if(cmd_access(id,level,cid, 1)){
			read_argv(1, pass, charsmax(pass);
			remove_quotes(pass);
			if(!pass[0]){
				client_print(id, print_console, "%L", LANG_PLAYER, "PASS_REQ");
				client_print(id, print_chat, "%L", LANG_PLAYER, "PASS_REQ");
			} else {
				nvault_set(gVaultRegister, name, pass);
				client_print(id, print_console, "%L", LANG_PLAYER, "NICK_REGISTERED", name, pass);
				client_print(id, print_console, "%L", LANG_PLAYER, "NICK_REGISTERED2");
				client_print(id, print_console, "%L", LANG_PLAYER, "NICK_REGISTERED3");
				client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_REGISTERED", name, pass);
				client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_REGISTERED2");
				client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_REGISTERED3");
			}
		}
	}
	return PLUGIN_HANDLED;
}
public cmdUnRegisterName(id) {
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isRegisterActive())
		return PLUGIN_CONTINUE;
		
	if(isNickRegistered(id)){
		set_hudmessage(255, 0, 0, 0.0, 0.03, 0, 6.0, 5.0);
		show_hudmessage(id, "%L", LANG_PLAYER, "ENTER_PASSWORD");
		client_cmd(id, "messagemode unregister_name_password");
	} else {
		client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_NOT_REGISTERED");
	}
	return PLUGIN_HANDLED;
}
public unregisterName(id, level, cid){
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isRegisterActive())
		return PLUGIN_CONTINUE;
	
	new pass[32];
	new name[MAX_NAME_LEN];
	copy(name, MAX_NAME_LEN -1, playerKey[id]);
	
	read_argv(1, pass2, charsmax(pass2);
	remove_quotes(pass2);
	
	if(getUserPass(id, pass, 31)){
		if(equal(pass,pass2)){
			nvault_remove(gVaultRegister, name);
			client_print(id, print_console, "%L", LANG_PLAYER, "NICK_UNREGISTERED", name);
			client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_UNREGISTERED", name);
		} else {
			client_print(id, print_console, "%L", LANG_PLAYER, "PASS_INVALID");
			client_print(id, print_chat, "%L", LANG_PLAYER, "PASS_INVALID");
		}
	} else {
		client_print(id, print_console, "%L", LANG_PLAYER, "NICK_NOT_REGISTERED", name);
		client_print(id, print_chat, "%L", LANG_PLAYER, "NICK_NOT_REGISTERED", name);
	}
	return PLUGIN_HANDLED;
}
public showNickRegisterMenu(id){
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isRegisterActive())
		return PLUGIN_CONTINUE;
	
	new title[64];
	formatex(title, charsmax(title), "%L", "TITLE_NICK_REGISTER");
	new menu = menu_create(title, "nickRegisterMenuHandler");
	
	formatex(title, charsmax(title), "%L", LANG_PLAYER, (isNickRegistered(id)?"UNREGISTER_NICK":"REGISTER_NICK")
	return PLUGIN_HANDLED;
}

public nickRegisterMenuHandler(id, menu, item){
	
}
